package com.sqx.modules.goodsMessage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.DateUtils;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.service.GoodsService;
import com.sqx.modules.goodsMessage.entity.GoodsMessage;
import com.sqx.modules.goodsMessage.dao.GoodsMessageDao;
import com.sqx.modules.goodsMessage.service.GoodsMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.utils.BaiDuUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import com.sqx.common.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 商品留言 服务实现类
 *
 * @author wuChang
 * @since 2024-06-03
 */
@Service
public class GoodsMessageServiceImp extends ServiceImpl<GoodsMessageDao, GoodsMessage> implements GoodsMessageService {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveGoodsMessage(GoodsMessage goodsMessage) {
        UserEntity userEntity = userService.getById(goodsMessage.getUserId());
        String value = commonInfoService.findOne(332).getValue();
        if ("2".equals(value)) {
            if (StringUtils.isNotBlank(goodsMessage.getMessageContent())) {
                if (BaiDuUtils.auditBaiDuText(goodsMessage.getMessageContent())) {
                    return Result.error("您发布的内容包含敏感词汇");
                }
            }
        }
        Goods goods = goodsService.getById(goodsMessage.getGoodId());
        if (goods == null || goods.getIsEnable() == 0) {
            return Result.error("商品不存在或已下架");
        }
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setContent("用户" + userEntity.getUserName() + "，给您留言《" + goodsMessage.getMessageContent() + "》");
        messageInfo.setTitle("留言通知");
        messageInfo.setState(String.valueOf(6));
        messageInfo.setUserId(goods.getUserId().toString());
        messageInfo.setCreateAt(DateUtils.format(new Date()));
        messageInfo.setIsSee("0");
        messageService.saveBody(messageInfo);
        goodsMessage.setCreateTime(LocalDateTime.now());
        goodsMessage.setIsDelete(0);
        return Result.upStatus(baseMapper.insert(goodsMessage));
    }

    @Override
    public IPage<GoodsMessage> getGoodsMessageList(Integer page, Integer limit, GoodsMessage goodsMessage) {
        Page<GoodsMessage> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.getGoodsMessageList(pages, goodsMessage);
    }

    @Override
    public IPage<GoodsMessage> getAppGoodsMessageList(Integer page, Integer limit, GoodsMessage goodsMessage) {
        Page<GoodsMessage> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<GoodsMessage> goodsMessageIPage = baseMapper.getGoodsMessageList(pages, goodsMessage);
        for (GoodsMessage record : goodsMessageIPage.getRecords()) {
            record.setMessageId(record.getMessageId());
            List<GoodsMessage> goodsMessageList = baseMapper.getReplyGoodsMessageList(record);
            if (goodsMessageList != null) {
                record.setGoodsMessageList(goodsMessageList);
            }
        }
        return goodsMessageIPage;
    }

    @Override
    public Result deleteGoodsMessageById(Long userId, Long messageId) {
        GoodsMessage goodsMessage = baseMapper.selectById(messageId);
        if (goodsMessage == null) {
            return Result.error("留言内容不存在");
        }
        UserEntity userEntity = userService.getById(userId);
        if (userEntity == null) {
            return Result.error("用户信息已失效,请重新登录");
        }
        if (userId == null) {
            goodsMessage.setDeleteUser(3);
        } else if (userId.equals(goodsMessage.getUserId())) {
            goodsMessage.setDeleteUser(2);
        } else {
            Goods goods = goodsService.getById(goodsMessage.getGoodId());
            if (userId.equals(goods.getUserId())) {
                goodsMessage.setDeleteUser(1);
            } else {
                return Result.error("非发布人不可操作");
            }
        }
        goodsMessage.setIsDelete(1);
        int result = baseMapper.updateById(goodsMessage);
        if (result <= 0) {
            return Result.error("删除失败");
        } else {
            List<GoodsMessage> goodsMessage1 = baseMapper.selectList(new QueryWrapper<GoodsMessage>().eq("parent_message_id", goodsMessage.getMessageId()));
            if (goodsMessage1 != null) {
                for (GoodsMessage goodsMessage2 : goodsMessage1) {
                    goodsMessage2.setIsDelete(1);
                    baseMapper.updateById(goodsMessage2);
                }
            }
        }
        return Result.success();
    }

    @Override
    public Result replyGoodsMessage(GoodsMessage goodsMessage) {
        String value = commonInfoService.findOne(332).getValue();
        if ("2".equals(value)) {
            if (StringUtils.isNotBlank(goodsMessage.getMessageContent())) {
                if (BaiDuUtils.auditBaiDuText(goodsMessage.getMessageContent())) {
                    return Result.error("您回复的内容包含敏感词汇");
                }
            }
        }
        if (goodsMessage.getGoodId() != null) {
            Goods goods = goodsService.getById(goodsMessage.getGoodId());
            if (goods == null) {
                return Result.error("商品不存在，请刷新后重试");
            }
            if (goodsMessage.getByUserId() == null) {
                return Result.error("回复对象不能为空");
            }
            if (!goodsMessage.getUserId().equals(goods.getUserId())) {
                return Result.error("您无权操作");
            }
            GoodsMessage goodsMessage1 = new GoodsMessage();
            goodsMessage1.setParentMessageId(goodsMessage.getMessageId());
            goodsMessage1.setGoodId(goodsMessage.getGoodId());
            goodsMessage1.setUserId(goods.getUserId());
            goodsMessage1.setMessageContent(goodsMessage.getMessageContent());
            goodsMessage1.setByUserId(goodsMessage.getByUserId());
            goodsMessage1.setCreateTime(LocalDateTime.now());
            goodsMessage1.setIsDelete(0);
            int result = baseMapper.insert(goodsMessage1);
            if (result > 0) {
                UserEntity userEntity = userService.getById(goodsMessage.getUserId());
                if (userEntity != null) {
                    MessageInfo messageInfo = new MessageInfo();
                    messageInfo.setContent("用户" + userEntity.getUserName() + "，给您回复了《" + goodsMessage.getMessageContent() + "》");
                    messageInfo.setTitle("留言回复");
                    messageInfo.setState(String.valueOf(6));
                    messageInfo.setUserId(goodsMessage.getByUserId().toString());
                    messageInfo.setCreateAt(DateUtils.format(new Date()));
                    messageInfo.setIsSee("0");
                    messageService.saveBody(messageInfo);
                }
            } else {
                return Result.error("回复失败");
            }
            return Result.success();
        }
        return Result.error("商品不存在，请刷新后重试");
    }

    @Override
    public IPage<GoodsMessage> getReplyGoodsMessageList(Integer page, Integer limit, GoodsMessage goodsMessage) {
        Page<GoodsMessage> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<GoodsMessage> goodsMessageIPage = baseMapper.getAdminGoodsMessageList(pages, goodsMessage);
        return goodsMessageIPage;
    }

}
